<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="logo" />
      </div>
      <el-form  ref="loginForm" class="login_form" :model="loginForm" :rules="loginRules" >
        <!--代表后面跟的是一个对象，而不是字符串，注意颜色区分-->
  <el-form-item  prop="username">
    <el-input placeholder="请输入用户名" prefix-icon="el-icon-user" v-model="loginForm.username" ></el-input>
  </el-form-item>
  <el-form-item  prop="password" >
    <el-input placeholder="请输入密码"  v-model="loginForm.password"  show-password @keyup.enter.native="login"></el-input>
  </el-form-item>
  <el-form-item >
   <el-button type="primary" @click="login"> 登录 </el-button>
   <el-button type="info" @click="resetForm">重置</el-button>
  </el-form-item>
  </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 16, message: '长度在 4 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () { // form组件对象.resetFileds
      this.$refs.loginForm.resetFields()
      // this.$refs['loginForm'].resetFields()
    },
    login () {
      // console.log(this) // 当前组件自己
      // console.log(this.loginForm)
      // const { username, password } = this.loginForm
      // console.log(username, password)
      this.$refs.loginForm.validate(async (valid) => {
        // if (valid) {
        //   console.log('我要准备提交数据了')
        // }
        if (valid) {
          console.log('我要准备提交数据了')
          const { data: response } = await this.$http.post('login/', this.loginForm)
          console.log(response)
          if (response.code) { // code 不为0说明一定有异常
            const ret = this.$message.error(response.message)
            return ret
          }
          const token = response.access
          window.localStorage.setItem('token', token)

          // 这里就是正常的处理代码
          this.$message.success('登录成功')
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 35%;
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
